<template>
    <view class="content">
		<view class="u-m-b-20">
			<u-search placeholder="搜索内容" shape="round" :show-action="false" bg-color="#fff" v-model="keyword"
				@search="doSearch"></u-search>
		</view>
		<view class="address-btn" @click="$gTo('/pages/address/take-new')">
		    新建地址
		</view>
		
        <view class="address">
            <view class="address-list" v-for="(item, index) in address" :key="index">
                <view class="address-info" @click="choose(item)">
                    <view class="u-flex u-row-left u-col-center u-p-b-10">
                        <view>{{item.contacts}}</view>
                        <view class="u-p-l-25">{{item.phone}}</view>
                    </view>
                    <view class="u-flex u-row-left u-col-top u-p-b-10">
                        <view>经营位置：</view>
                        <view class="u-flex-1">{{item.address}}</view>
                    </view>
                    <view class="u-flex u-row-left u-col-top u-p-b-10">
                        <view>收货方：</view>
                        <view class="u-flex-1">{{item.name}}</view>
                    </view>
                </view>
                <view class="operation u-flex u-row-between" v-if="item.status == 1">
                    <view class="choice u-flex">
                        <u-checkbox @change="checkChange(item)" v-model="item.is_default==1?true:false"  
                            size="32" active-color="#ff602c" label-size="26" shape="circle">设为默认</u-checkbox>
                    </view>
                    <view class="right u-flex">
                        <view class="u-flex" @click.stop="openPage('/pages/address/take-new',item.id)">
                            <image src="../../static/icon/write.png" mode=""></image>
                            <text>编辑</text>
                        </view>
                        <view class="u-flex u-m-l-40" @click.stop="delAddress(item.id)">
                            <image src="../../static/icon/del_02.png" mode=""></image>
                            <text>删除</text>
                        </view>
                    </view>
                </view>
            </view>
			
			<view style="height: 94rpx;padding-top: 30rpx;">
			    <u-loadmore :status="status" v-if="status != 'loadmore'" />
			</view>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
				keyword: '',
                address: [],
				status: 'loadmore', //是否显示没有更多了
				page: 1,
				from: '',
            };
        },
		onLoad(option) {
			if (option.from) {
				//零担进入选择
				this.from = option.from
			}
		},
        onShow() {
        	this.address = []
        	this.page = 1
        	this.status = 'loadmore'
        	this.getAddress()
        },
        onReachBottom() {
        	if(this.page == 1){return}
        	this.getAddress()
        },
        methods: {
			// 选择地址
			choose(item) {
				console.log('this.from',this.from)
				if (this.from) {
					uni.$emit('CHOOSE_TAKE_ADD', item)
				} else {
					uni.$emit('CHOOSE_SEND_ADD', item)
				}
				setTimeout(() => {
					this.$backT()
				}, 500)
			},
			
			doSearch() {
				this.address = []
				this.page = 1
				this.status = 'loadmore'
				this.getAddress()
			},
			
            // 获取地址列表
			getAddress() {
				if (this.status == 'nomore') return;
				this.status = 'loading';
				this.$ajax('files_address', {
					user_token: this.$getSync('userToken'),
					type: 2,//1发货方 2收货方
					status: 2,//1自己的 2自己的+平台的
					id: '',//地址id
					name: this.keyword,//搜索
					page: this.page,
					limit: 10,
				}).then(ret => {
					if (ret.success == 1000) {
						if (ret.detail && ret.detail.length > 0) {
							this.address = this.address.concat(ret.detail);
							this.page++;
							this.status = 'loadmore'
						} else {
							this.status = 'nomore'
						}
					} else {
						this.$toast(ret.msg);
					}
				});
			},
			
            // 设为默认地址
            checkChange(item){
            	this.$ajax('update_is_default', {
            		user_token: this.$getSync('token'),
            		id: item.id,//地址id
                    is_default: item.is_default == 1 ? 0 : 1,//1是 0否
                }).then(ret => { 	
                    if (ret.success == 1000) {
                        this.address = []
                        this.page = 1
                        this.status = 'loadmore'
                        this.getAddress()
                    } else {
                        this.$toast(ret.msg);
                    }
            	})
            },
			
            delAddress(id) {
            	var that = this
            	uni.showModal({
                    title: '温馨提示',
            		content: '您确定删除此地址吗？',
            		confirmText: "确认",
            		cancelText: '取消',
            		confirmColor: '#f29700',
            		success: (res) => {
            			if (res.confirm) {
            				this.$ajax('delete_files_address', {
            					user_token: this.$getSync('token'),
            					id: id
            				}).then(ret => { 
                                if (ret.success == 1000) {
                                	that.address = []
                                	that.page = 1
                                	that.status = 'loadmore'
                                	that.getAddress()
                                	that.$toast('删除成功！')
                                } else {
                                	that.$toast(ret.msg);
                                }
            				})
            			} else {
            			}
            		}
            	})
            },
			
            openPage(str,id){
            	uni.navigateTo({
                    url: str + "?id=" + id
            	})
            },
        }
    };
</script>

<style>
    page {
        background-color: #eef1f6;
    }
</style>

<style lang="scss" scoped>
    
    .content {
        padding: 20rpx;
    }
    
    .address {
        width: 100%;

        .address-list {
            width: 100%;
            background-color: #ffffff;
            border-radius: 15rpx;
            margin-bottom: 20rpx;

            .address-info {
                width: 100%;
                padding: 35rpx 20rpx;
                font-weight: bold;
            }

            // 操作
            .operation {
                width: 100%;
                padding: 0 20rpx 35rpx;

                .choice,
                .right {
                    font-size: 26rpx;
                    color: #313131;

                    image {
                        display: block;
                        width: 32rpx;
                        height: 32rpx;
                    }

                    text {
                        margin-left: 12rpx;
                    }
                }

            }
        }
    }


    .address-btn {
        font-size: 34rpx;
        color: #fff;
        text-align: center;
        padding: 25rpx;
        background-color: #00457f;
        border-radius: 50rpx;
        margin: 30rpx auto;
    }
</style>
